﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Interop.Security.AzRoles;
using System.Runtime.InteropServices;

namespace Authorization
{


//    typedef enum _SID_NAME_USE {
//  SidTypeUser             = 1,
//  SidTypeGroup,
//  SidTypeDomain,
//  SidTypeAlias,
//  SidTypeWellKnownGroup,
//  SidTypeDeletedAccount,
//  SidTypeInvalid,
//  SidTypeUnknown,
//  SidTypeComputer,
//  SidTypeLabel 
//} SID_NAME_USE, *PSID_NAME_USE;

    //[ComVisible(true), ClassInterface((short)0), Guid("9030BAA9-B3E5-4cf2-BB59-139ED0C9B8FA"), TypeLibType((short)2)]
    [Guid("DF989C8F-7F38-4bd5-B7EA-B195F205F340")]
    public class CustomNameResolver : IAzNameResolver
    {
        #region IAzNameResolver Members
        public string NameFromSid(string bstrSid, out int pSidType)
        {
         //   Microsoft.Interop.Security.AzRoles.AzPrincipalLocatorClass 
         System.Web.Security.MembershipUser user = System.Web.Security.Membership.GetUser(bstrSid, false);

         pSidType = 1;
         if (user != null)
             return user.UserName;
         else
             return null;
        }


        public object NamesFromSids(object vSids, out object pvSidTypes)
        {
           List<string> userNames = new List<string>();
            List<int> SidTypes = new List<int>();

            foreach (string vSid in (string[])vSids)
	        {
               int sidType; 
    	       var user = NameFromSid(vSid, out sidType);
               if (user!=null)
               {
                   userNames.Add(user);
                   SidTypes.Add(sidType);
               }
            }
            pvSidTypes = SidTypes.ToArray();

            return userNames.ToArray();
        }

        #endregion
    }
}
